<!--
 * @Author: HeAo
 * @Date: 2021-10-11 11:20:48
 * @LastEditTime: 2021-10-11 11:50:57
 * @LastEditors: HeAo
 * @Description: 
 * @FilePath: \web2103-2105\web2105\3-2-Vue\day03\3-组件通信-子传父.html
 * 别乱动！
-->
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <script src="../js/vue.js"></script>
  <style>
    .box {
      width: 300px;
      height: 50px;
      font-size: 20px;
      color: red;
      border: 1px solid #333;
    }
  </style>
</head>

<body>
  <div id="app">
    <div class="box">
      {{parentData}}
    </div>
    <!-- 父组件接收子组件向上发送的事件，通过v-on:child-event="" -->
    <my-button @child-event="parentHandler">
      成功按钮
    </my-button>
    <my-button @child-event="parentHandler">
      失败按钮
    </my-button>
  </div>
  <script>
    let component1 = {
      data() {
        return {
          msg: '子组件数据'
        }
      },
      template: `
        <div>
          <h3>
            <slot></slot>
          </h3>
          <button>{{msg}}</button>
          <button @click="toEmitHandler">发送事件</button>
        </div>
      `,
      methods: {
        toEmitHandler() {
          // 1.子组件通过this.$emit向上发送事件
          this.$emit('child-event', this.msg)
        }
      }
    }
    let vm = new Vue({
      el: '#app',
      data: {
        parentData: '我是父组件数据'
      },
      methods: {
        // 3.在触发父组件的事件处理函数中。可以获取到子组件向上携带的参数
        parentHandler(data) {
          console.log('子组件传递过来的数据：', data);
          this.parentData = data
        }
      },
      created() {

      },
      components: {
        'my-button': component1
      }
    })
  </script>
</body>

</html>